package com.mall.user.services;

import com.mall.commons.tool.exception.ValidateException;
import com.mall.user.IVerifyService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.entitys.UserVerify;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dal.persistence.UserVerifyMapper;
import com.mall.user.dto.UserVerifyRequest;
import com.mall.user.dto.UserVerifyResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.math.BigInteger;
import java.util.List;

/**
 * @author Connor
 * @date 2020/12/21
 * @Version 1.0
 */
@Slf4j
@Component
@Service(interfaceClass = IVerifyService.class)
public class VerifyServiceImpl implements IVerifyService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private UserVerifyMapper userVerifyMapper;

    @Override
    public UserVerifyResponse verifyUser(UserVerifyRequest userVerifyRequest) {
        UserVerifyResponse userVerifyResponse = new UserVerifyResponse();

        try {
            userVerifyRequest.requestCheck();
        } catch (ValidateException e) {
            userVerifyResponse.setCode(e.getErrorCode());
            userVerifyResponse.setMsg(e.getMessage());
            return userVerifyResponse;
        }

        String uuid = userVerifyRequest.getUuid();
        String userName = userVerifyRequest.getUserName();
        Example memberExample = new Example(Member.class);
        memberExample.createCriteria().andEqualTo("username", userName);
        Member member = new Member();

        Example example = new Example(UserVerify.class);
        example.createCriteria().andEqualTo("uuid", uuid).andEqualTo("username", userName);
        UserVerify userVerify = userVerifyMapper.selectOneByExample(example);
        String isExpire = null;
        String isVerify = null;
        if (userVerify != null) {
            isExpire = userVerify.getIsExpire();
            isVerify = userVerify.getIsVerify();
        }
        Boolean hasExpired = SysRetCodeConstants.VERIFY_ISEXPIRE_Y.getCode().equals(isExpire);
        Boolean hasVerified = SysRetCodeConstants.USER_ISVERIFIED_Y.getCode().equals(isVerify);
        if (hasExpired) {
            userVerifyResponse.setCode(SysRetCodeConstants.VERIFY_ISEXPIRE_ERROR.getCode());
            userVerifyResponse.setMsg(SysRetCodeConstants.VERIFY_ISEXPIRE_ERROR.getMessage());
            return userVerifyResponse;
        }
        if (isVerify != null) {
            userVerifyResponse.setIsverified(isVerify);
            member.setIsVerified(isVerify);
            memberMapper.updateByExampleSelective(member, memberExample);
            if (hasVerified) {
                userVerifyResponse.setCode(SysRetCodeConstants.USER_ISVERIFIED_CORRECT.getCode());
                userVerifyResponse.setMsg(SysRetCodeConstants.USER_ISVERIFIED_CORRECT.getMessage());
                return userVerifyResponse;
            }
        }
        userVerifyResponse.setCode(SysRetCodeConstants.USER_ISVERFIED_ERROR.getCode());
        userVerifyResponse.setMsg(SysRetCodeConstants.USER_ISVERFIED_ERROR.getMessage());

        return userVerifyResponse;
    }
}
